package com.netflix.appinfo;

import com.netflix.discovery.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Deprecated
public class DiscoveryManager {

    private DiscoveryClient discoveryClient;
    private EurekaInstanceConfig eurekaInstanceConfig;
    private EurekaClientConfig eurekaClientConfig;
    private static final DiscoveryManager s_instance = new DiscoveryManager();

    private DiscoveryManager() {
    }


    public static DiscoveryManager getInstance() {
        return s_instance;
    }

    public void setDiscoveryClient(DiscoveryClient discoveryClient) {
        this.discoveryClient = discoveryClient;
    }

    public void setEurekaClientConfig(EurekaClientConfig eurekaClientConfig) {
        this.eurekaClientConfig = eurekaClientConfig;
    }

    public void setEurekaInstanceConfig(EurekaInstanceConfig eurekaInstanceConfig) {
        this.eurekaInstanceConfig = eurekaInstanceConfig;
    }

    public void initComponent(EurekaInstanceConfig config, EurekaClientConfig eurekaConfig, AbstractDiscoveryClientOptionalArgs args) {
        this.eurekaInstanceConfig = config;
        this.eurekaClientConfig = eurekaConfig;
        if (ApplicationInfoManager.getInstance().getInfo() == null) {
            // Initialize application info
            ApplicationInfoManager.getInstance().initComponent(config);
        }
        InstanceInfo info = ApplicationInfoManager.getInstance().getInfo();
        discoveryClient = new DiscoveryClient(info, eurekaConfig, args);
    }

    public void initComponent(EurekaInstanceConfig config, EurekaClientConfig eurekaConfig) {
        initComponent(config, eurekaConfig, null);
    }

    public void shutdownComponent() {
        if (discoveryClient != null) {
            try {
                discoveryClient.shutdown();
                discoveryClient = null;
            } catch (Throwable th) {
                log.error("Error in shutting down client", th);
            }
        }
    }

    public LookupService getLookupService() {
        return discoveryClient;
    }

    @Deprecated
    public DiscoveryClient getDiscoveryClient() {
        return discoveryClient;
    }

    public EurekaClient getEurekaClient() {
        return discoveryClient;
    }

    public EurekaClientConfig getEurekaClientConfig() {
        return eurekaClientConfig;
    }

    public EurekaInstanceConfig getEurekaInstanceConfig() {
        return eurekaInstanceConfig;
    }
}
